package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.AttendanceDao;
import com.example.domain.Attendance;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.AttendanceService;
import com.example.tool.DateTool;

/**
 * @author 袁小弘
 * @describe 考勤服务实现层
 * @date 2019-12-31
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    AttendanceDao attendanceDao;

    /**
     * @describe 添加考勤信息
     */
    @Override
    public Result addAttendance(Attendance attendance) {
        if (attendance != null) {
            //获取系统当前时间作为记录创建时间
//			attendance.setAttendanceTime(DateTool.getTimestamp());
            attendance.setRecordTime(DateTool.getTimestamp());
        }
        //调用JPA保存实体方法
        Attendance attendance2 = attendanceDao.save(attendance);
        if (attendance2 != null) {
            return ResultFactory.buildSuccessResult(attendance2);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 更新考勤信息
     */
    @Override
    public Result updateAttendance(Attendance attendance) {
        if (attendance != null) {
            //获取系统当前时间作为记录更新时间
            attendance.setUpdateTime(DateTool.getTimestamp());
        }
        //调用JPA保存并刷新实体方法
        Attendance attendance2 = attendanceDao.saveAndFlush(attendance);
        if (attendance2 != null) {
            return ResultFactory.buildSuccessResult(attendance2);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除考勤信息
     */
    @Override
    public Result deleteAttendanceById(String attendanceId) {
        int row = attendanceDao.deleteByAttendanceId(Integer.parseInt(attendanceId));
        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 查询所有考勤信息
     */
    @Override
    public Result queryAllAttendance() {
        List<Attendance> list = attendanceDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 根据编号查询考勤信息
     */
    @Override
    public Result queryAttendanceById(String attendanceId) {
        List<Attendance> list = attendanceDao.findByAttendanceId(Integer.parseInt(attendanceId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加考勤信息
     */
    @Override
    public Result addAttendanceByExcel(List<Attendance> list) {
        List<Attendance> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = attendanceDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 考勤信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;
        if (info[0].equals("attendanceTime")) {

            list = attendanceDao.findByAttendanceTimeLike(info[1]);

        } else if (info[0].equals("attendanceStatus")) {

            list = attendanceDao.findByAttendanceStatusLike(info[1]);

        } else if (info[0].equals("userName")) {

            list = attendanceDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = attendanceDao.findByNickNameLike(info[1]);

        }


        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据考勤编号批量删除考勤信息
     */
    @Override
    public Result deleteAttendanceServiceByIds(Map<String, String> attendanceIds) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (attendanceIds != null) {
            String ids = attendanceIds.get("Ids");
            String[] id = ids.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = attendanceDao.deleteByAttendanceIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学生的考勤信息
     */
    @Override
    public Result queryAllStudentAndAttendanceInfo() {
        // TODO 查询所有学生的考勤信息
        List<Map<String, Object>> list = attendanceDao.queryAllStudentAndAttendanceInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result selectAttendanceByUserId(Integer userId) {
        // TODO 根据学生ID查询学生考勤信息
        List<Map<String, Object>> list = attendanceDao.selectAttendanceByUserId(userId);
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }
}
